/*
 * Sesame Inc.
 * Copyright (c) 2009 All Rights Reserved.
 */
package com.sesame.serviceimpl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.ui.Model;
import com.sesame.dao.AlbumDAO;
import com.sesame.dao.FriendDAO;
import com.sesame.dao.GuestbookDAO;
import com.sesame.dao.MessageDAO;
import com.sesame.dao.UserDAO;
import com.sesame.domain.Album;
import com.sesame.domain.Friend;
import com.sesame.domain.Guestbook;
import com.sesame.domain.Message;
import com.sesame.domain.User;
import com.sesame.service.ContactService;

/**
 * @author voidman
 * 
 */
public class ContactServiceImpl implements ContactService {

    private User user;
    private Friend friend;
    private UserDAO userDAO;
    private FriendDAO friendDAO;
    private MessageDAO messageDAO;
    private GuestbookDAO guestbookDAO;
    private Guestbook guestbook;

    public void setFriend(Friend friend) {
        this.friend = friend;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }

    public void setFriendDAO(FriendDAO friendDAO) {
        this.friendDAO = friendDAO;
    }

    public void setMessageDAO(MessageDAO messageDAO) {
        this.messageDAO = messageDAO;
    }

    public void setGuestbookDAO(GuestbookDAO guestbookDAO) {
        this.guestbookDAO = guestbookDAO;
    }

    public void setGuestbook(Guestbook guestbook) {
        this.guestbook = guestbook;
    }

    public List<User> listAllUser() {
        return userDAO.getAllUser();
    }

    public List<Friend> listAllFriend(Integer user1Id) {
        return friendDAO.getAllFriend(user1Id);
    }

    public void makeFriend(Integer user1Id, Integer user2Id, String headPic) {
        friend.setSenderId(user1Id);
        friend.setReceiverId(user2Id);
        friend.setHeadPic(headPic);
        String receiver = userDAO.getUserById(user2Id).getNickName();
        friend.setReceiver(receiver);

        if (friendDAO.getFriendByUserId(friend) != null) {
            throw new RuntimeException("非法操作");
        }
        friendDAO.addFriend(friend);
        sendMessage(user1Id, user2Id);
    }

    public boolean isFriend(Integer user1Id, Integer user2Id) {
        friend.setSenderId(user1Id);
        friend.setReceiverId(user2Id);
        if (friendDAO.getFriendByUserId(friend) == null) {
            return false;
        } else {
            return true;
        }
    }

    public void deleteFriend(Integer user1Id, Integer user2Id) {
        friend.setSenderId(user1Id);
        friend.setReceiverId(user2Id);
        if (friendDAO.getFriendByUserId(friend) == null) {
            throw new RuntimeException("非法操作");
        }
        friendDAO.deleteFriendByUserId(friend);
    }

    public List<Guestbook> listAllGuestbook(Integer hostUserId) {
        return guestbookDAO.getAllGuestbook(hostUserId);

    }

    public void newGuestbook(Integer userId, Integer hostUserId,
            String content, String nickName, String headPic) {
        guestbook.setUserId(userId);
        guestbook.setHostUserId(hostUserId);
        guestbook.setContent(content);
        guestbook.setNickName(nickName);
        guestbook.setHeadPic(headPic);
        Date createTime = new Date();
        guestbook.setCreateTime(createTime);

        guestbookDAO.addGuestbook(guestbook);

    }

    public void deleteGuestbook(Integer id) {
        guestbookDAO.deleteGuestbookById(id);
    }

    public void reply(String reply, Integer guestbookId) {
        guestbook.setId(guestbookId);
        System.out.println(guestbook.getId());
        guestbook.setReply(reply);
        Date replyTime = new Date();
        guestbook.setReplyTime(replyTime);
        guestbookDAO.updateGuestbook(guestbook);
    }

    private void sendMessage(Integer user1Id, Integer user2Id) {
        Message message = new Message();
        message.setSenderId(user1Id);
        message.setReceiverId(user2Id);
        message.setSender(userDAO.getUserById(user1Id).getNickName());
        message.setReceiver(userDAO.getUserById(user2Id).getNickName());
        message.setTitle("我已经加你为好友");
        message.setContent("我已经加你为好友，你可以浏览我的半公开图片，如果你有兴趣，也可以加我为好友!");
        message.setState("0");
        message.setFlag("both");
        message.setSendTime(new Date());
        messageDAO.addMessage(message);

    }

    public List<Friend> listTopFriend(Integer user1Id) {
        return friendDAO.getTopFriend(user1Id);
    }

    public List<Guestbook> listTopGuestbook(Integer hostUserId) {
        return guestbookDAO.getTopGuestbook(hostUserId);

    }

}